package com.tan.user.service.impl;

import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.core.util.IdUtil;
import com.tan.common.mapper.user.SysUserMapper;
import com.tan.common.vo.auth.CaptchaResult;
import com.tan.core.enums.RedisKeyEnum;
import com.tan.core.utils.RedissonTool;
import com.tan.user.service.IAuthService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.concurrent.TimeUnit;

import static java.awt.Font.SANS_SERIF;

/**
 * @author Tan
 * @description
 * @date 2025/2/9
 */

@Slf4j
@Service
public class AuthServiceImpl implements IAuthService {

    @Resource
    private RedissonTool redissonTool;

    @Resource
    private SysUserMapper userMapper;


    @Override
    public CaptchaResult getCaptcha() {
        MathGenerator mathGenerator = new MathGenerator(1);
        CircleCaptcha circleCaptcha = new CircleCaptcha(200, 35, 4, 3);
        circleCaptcha.setGenerator(mathGenerator);
        circleCaptcha.setFont(new Font(SANS_SERIF, Font.BOLD, 18));
        String captchaCode = circleCaptcha.getCode(); // 验证码
        String captchaBase64 = circleCaptcha.getImageBase64Data(); // 验证码图片Base64
        // 验证码文本缓存至Redis，用于登录校验
        String captchaKey = IdUtil.fastSimpleUUID();
        String key = RedisKeyEnum.LOGIN_CAPTCHA.createKey(captchaKey);
        redissonTool.cacheObj(key, captchaCode, 120);
        return CaptchaResult.builder()
                .captchaKey(captchaKey)
                .captchaBase64(captchaBase64)
                .build();
    }
}
